<?php

namespace Xin\Wechat\Laravel;

use Illuminate\Foundation\Application as LaravelApplication;
use Illuminate\Support\ServiceProvider;
use Laravel\Lumen\Application as LumenApplication;
use Xin\Wechat\Contracts\Factory as WechatFactory;
use Xin\Wechat\WechatManager;
use Xin\Wechat\WechatMediaManager;
use Xin\Wechat\WechatUserSessionManager;

class WechatServiceProvider extends ServiceProvider
{
	/**
	 * Register any application services.
	 *
	 * @return void
	 */
	public function register()
	{
		// 注册微信实例工厂
		$this->registerWechatFactory();

		// 注册微信相关实例
		$this->registerWechatApplications();

		// 注册微信素材管理服务
		$this->registerWechatMediaManager();

		// 注册微信 用户Session 管理服务
		$this->registerWechatUserSessionManager();
	}

	/**
	 * 注册微信实例工厂
	 * @return void
	 */
	protected function registerWechatFactory()
	{
		$this->app->singleton(WechatManager::class, function ($app) {
			return new WechatManager(config('wechat') ?: []);
		});
		$this->app->alias(WechatManager::class, 'wechat');
		$this->app->alias(WechatManager::class, WechatFactory::class);
	}

	/**
	 * 注册微信实例
	 * @return void
	 */
	protected function registerWechatApplications()
	{
		$this->app->singleton('wechat.miniprogram', function ($app) {
			return $app['wechat']->miniProgram();
		});

		$this->app->singleton('wechat.miniapp', function ($app) {
			return $app['wechat']->miniApp();
		});

		$this->app->singleton('wechat.official_account', function ($app) {
			return $app['wechat']->officialAccount();
		});

		$this->app->singleton('wechat.open_platform', function ($app) {
			return $app['wechat']->openPlatform();
		});

		$this->app->singleton('wechat.work', function ($app) {
			return $app['wechat']->work();
		});

		$this->app->singleton('wechat.open_work', function ($app) {
			return $app['wechat']->openWork();
		});
	}

	/**
	 * 注册微信素材服务
	 * @return void
	 */
	protected function registerWechatMediaManager()
	{
		WechatMediaManager::setDefaultCacheResolver(static function () {
			return app('cache');
		});

		$this->app->singleton(WechatMediaManager::class, function ($app) {
			return new WechatMediaManager(
				$this->app['wechat'],
				$this->app->cache
			);
		});
		$this->app->alias(WechatMediaManager::class, 'wechat.media');
	}

	/**
	 * 注册微信 用户Session 管理服务
	 * @return void
	 */
	protected function registerWechatUserSessionManager()
	{
		WechatUserSessionManager::setDefaultCacheResolver(static function () {
			return app('cache');
		});

		$this->app->singleton(WechatUserSessionManager::class, function ($app) {
			return new WechatUserSessionManager(
				$this->app['cache']
			);
		});
		$this->app->alias(WechatUserSessionManager::class, 'wechat.user_session');
	}

	/**
	 * Register the config for publishing
	 *
	 */
	public function boot()
	{
		if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
			$this->publishes([$this->configPath() => config_path('wechat.php')], 'wechat');
		} elseif ($this->app instanceof LumenApplication) {
			$this->app->configure('wechat');
		}
	}

	/**
	 * Set the config path
	 *
	 * @return string
	 */
	protected function configPath()
	{
		return __DIR__ . '/../../config/config.php';
	}
}
